package com.brossdong.socket.netty.chaibao;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * Created by brossdong on 2018/2/22.
 */
public class Client {

    public static void main(String[] args) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();

        Bootstrap b = new Bootstrap();

        b.group(group)
         .channel(NioSocketChannel.class)
         .handler(new ChannelInitializer<SocketChannel>() {
             @Override
             protected void initChannel(SocketChannel sc) throws Exception {
                 ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());
                 sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf));
                 sc.pipeline().addLast(new StringDecoder());
                 sc.pipeline().addLast(new ClientHandler());
             }
         });

        ChannelFuture future = b.connect("127.0.0.1", 8765).sync();

        //写数据
        future.channel().writeAndFlush(Unpooled.copiedBuffer("aaaaaa$_".getBytes()));
        future.channel().writeAndFlush(Unpooled.copiedBuffer("bbbbbb$_".getBytes()));
        future.channel().writeAndFlush(Unpooled.copiedBuffer("cccccc$_".getBytes()));

        future.channel().closeFuture().sync();
        group.shutdownGracefully();
    }

}
